<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="使用阶段" prop="usagePhase">
        <el-select v-model="queryParams.usagePhase" placeholder="请选择使用阶段" clearable>
          <el-option
            v-for="dict in dict.type.tb_usage_phase"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="建材名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入建材名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入用量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建材碳因" prop="carbonFactor">
        <el-input
          v-model="queryParams.carbonFactor"
          placeholder="请输入建材碳排放因子"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="因子单位" prop="factorUnit">
        <el-input
          v-model="queryParams.factorUnit"
          placeholder="请输入因子单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输方式" prop="transportMode">
        <el-input
          v-model="queryParams.transportMode"
          placeholder="请输入运输方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输距离" prop="transportDistance">
        <el-input
          v-model="queryParams.transportDistance"
          placeholder="请输入运输距离(km)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输因子" prop="transportFactor">
        <el-input
          v-model="queryParams.transportFactor"
          placeholder="请输入运输方式排放因子(kgCO2e/t*km)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="碳排放量" prop="totalEmission">
        <el-input
          v-model="queryParams.totalEmission"
          placeholder="请输入碳排放量(kgCO2e)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:masterialTransport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:masterialTransport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:masterialTransport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="upload.open = true"
          v-hasPermi="['temu:goods:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:masterialTransport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="masterialTransportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表ID" align="center" prop="materialId" v-if="columns[0].visible" />
      <el-table-column label="使用阶段" align="center" prop="usagePhase" v-if="columns[1].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.tb_usage_phase" :value="scope.row.usagePhase"/>
        </template>
      </el-table-column>
      <el-table-column label="建材名称" align="center" prop="materialName" v-if="columns[2].visible" />
      <el-table-column label="用量" align="center" prop="quantity" v-if="columns[3].visible" />
      <el-table-column label="单位" align="center" prop="unit" v-if="columns[4].visible" />
      <el-table-column label="建材碳排放因子" align="center" prop="carbonFactor" v-if="columns[5].visible" />
      <el-table-column label="因子单位" align="center" prop="factorUnit" v-if="columns[6].visible" />
      <el-table-column label="运输方式" align="center" prop="transportMode" v-if="columns[7].visible" />
      <el-table-column label="运输距离(km)" align="center" prop="transportDistance" v-if="columns[8].visible" />
      <el-table-column label="运输方式排放因子(kgCO2e/t*km)" align="center" prop="transportFactor" v-if="columns[9].visible" />
      <el-table-column label="碳排放量(kgCO2e)" align="center" prop="totalEmission" v-if="columns[10].visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:masterialTransport:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:masterialTransport:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改材料运输信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="使用阶段" prop="usagePhase">
          <el-select v-model="form.usagePhase" placeholder="请选择使用阶段">
            <el-option
              v-for="dict in dict.type.tb_usage_phase"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建材名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入建材名称" />
        </el-form-item>
        <el-form-item label="用量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入用量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="建材碳排放因子" prop="carbonFactor">
          <el-input v-model="form.carbonFactor" placeholder="请输入建材碳排放因子" />
        </el-form-item>
        <el-form-item label="因子单位" prop="factorUnit">
          <el-input v-model="form.factorUnit" placeholder="请输入因子单位" />
        </el-form-item>
        <el-form-item label="运输方式" prop="transportMode">
          <el-input v-model="form.transportMode" placeholder="请输入运输方式" />
        </el-form-item>
        <el-form-item label="运输距离(km)" prop="transportDistance">
          <el-input v-model="form.transportDistance" placeholder="请输入运输距离(km)" />
        </el-form-item>
        <el-form-item label="运输方式排放因子(kgCO2e/t*km)" prop="transportFactor">
          <el-input v-model="form.transportFactor" placeholder="请输入运输方式排放因子(kgCO2e/t*km)" />
        </el-form-item>
        <el-form-item label="碳排放量(kgCO2e)" prop="totalEmission">
          <el-input v-model="form.totalEmission" placeholder="请输入碳排放量(kgCO2e)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入材料运输信息对话框 -->
    <excel-upload
      :title="upload.title"
      :uploadUrl="upload.url"
      :open="upload.open"
      @uploadClose="upload.open = false"
      @handleFileSuccess="getList">
    </excel-upload>
  </div>
</template>

<script>
import { listMasterialTransport, getMasterialTransport, addMasterialTransport, updateMasterialTransport, delMasterialTransport } from "@/api/business/masterialTransport"

export default {
  name: "MasterialTransport",
  dicts: ['tb_usage_phase'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料运输信息表格数据
      masterialTransportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入Excel参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "材料运输信息导入",
        // 上传的地址
        url: "/business/masterialTransport/importData"
      },
      // 显隐列
      columns: [
          { key: 1, label: `表ID`, visible: true },
          { key: 2, label: `使用阶段`, visible: true },
          { key: 3, label: `建材名称`, visible: true },
          { key: 4, label: `用量`, visible: true },
          { key: 5, label: `单位`, visible: true },
          { key: 6, label: `建材碳排放因子`, visible: true },
          { key: 7, label: `因子单位`, visible: true },
          { key: 8, label: `运输方式`, visible: true },
          { key: 9, label: `运输距离(km)`, visible: true },
          { key: 10, label: `运输方式排放因子(kgCO2e/t*km)`, visible: true },
          { key: 11, label: `碳排放量(kgCO2e)`, visible: true },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usagePhase: null,
        materialName: null,
        quantity: null,
        unit: null,
        carbonFactor: null,
        factorUnit: null,
        transportMode: null,
        transportDistance: null,
        transportFactor: null,
        totalEmission: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        usagePhase: [
          { required: true, message: "使用阶段不能为空", trigger: "change" }
        ],
        materialName: [
          { required: true, message: "建材名称不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        factorUnit: [
          { required: true, message: "因子单位不能为空", trigger: "blur" }
        ],
        transportMode: [
          { required: true, message: "运输方式不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询材料运输信息列表 */
    getList() {
      this.loading = true
      listMasterialTransport(this.queryParams).then(response => {
        this.masterialTransportList = response.rows
        this.total = Number(response.total)
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        materialId: null,
        usagePhase: null,
        materialName: null,
        quantity: null,
        unit: null,
        carbonFactor: null,
        factorUnit: null,
        transportMode: null,
        transportDistance: null,
        transportFactor: null,
        totalEmission: null,
        createById: null,
        createBy: null,
        createTime: null,
        updateById: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加材料运输信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const materialId = row.materialId || this.ids
      getMasterialTransport(materialId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改材料运输信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialId != null) {
            updateMasterialTransport(this.form.materialId, this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMasterialTransport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids
      this.$modal.confirm('是否确认删除材料运输信息编号为"' + materialIds + '"的数据项？').then(function() {
        return delMasterialTransport(materialIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/masterialTransport/export', {
        ...this.queryParams
      }, `masterialTransport_export_${this.parseTime(new Date())}.xlsx`)
    }
  }
}
</script>
